//
//  SISSMyReleaseCooperationViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/4.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMyReleaseCooperationViewController.h"
#import "SISSMineHouseCell.h"
#import "SISSHouseCooperationModel.h"
#import "SISSNetworkingManager+MineCooperation.h"
#import "SISSReceivedRequestViewController.h"

static NSString *myReleaseHouseCooperationCellReuseID = @"myReleaseHouseCooperationCellReuseID";

@interface SISSMyReleaseCooperationViewController ()

@property (weak, nonatomic) IBOutlet UIView *containerView;

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/** 房源数组 */
@property (copy, nonatomic) NSArray<SISSHouseCooperationModel *> *houseResourceArr;


@end

@implementation SISSMyReleaseCooperationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestMyReleaseHouseCooperationData];
}


#pragma mark - 下拉刷新加载方法
- (void)upDownLoadData {
    
    [self requestMyReleaseHouseCooperationData];
}


#pragma mark - 加载数据
- (void)requestMyReleaseHouseCooperationData {
    
    [kNetworkManager requestMyReleaseHouseCooperationWithsuccess:^(NSArray<SISSHouseCooperationModel *> *houseResources) {
        
        self.houseResourceArr = houseResources;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            
        }];
        
    } failed:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.houseResourceArr.count];
    return self.houseResourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SISSMineHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:myReleaseHouseCooperationCellReuseID forIndexPath:indexPath];
    
    cell.houseCooperationModel = self.houseResourceArr[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSReceivedRequestViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"SISSReceivedRequestViewController"];
    vc.ahcrId = self.houseResourceArr[indexPath.row].ID;
    vc.deleteComplete = ^{
        
        [self requestMyReleaseHouseCooperationData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSMineHouseCell" bundle:nil] forCellReuseIdentifier:myReleaseHouseCooperationCellReuseID];
   
}

#pragma mark - Getter & Setter
- (NSArray<SISSHouseCooperationModel *> *)houseResourceArr {
    if (!_houseResourceArr) {
        _houseResourceArr = [NSMutableArray array];
    }
    return _houseResourceArr;
}


#pragma mark - 点击segmentControl的响应事件
- (IBAction)segmentControlClickAction:(UISegmentedControl *)sender {
    
    self.containerView.hidden = !sender.selectedSegmentIndex;
    
    NSLog(@"selectedSegmentIndex:  %zd",sender.selectedSegmentIndex);
}

@end
